﻿using System;


namespace Dynamic.Core.Extentions
{
    /// <summary>
    /// 字符串扩展方法
    /// </summary>
    public static class StringExtentions
    {
        /// <summary>
        /// 转化成易读的字符串(英文）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="isFirstUpper"></param>
        /// <returns></returns>
        public static string ToFriendlyString(this string str, bool isFirstUpper)
        {
            return "";
        }

        /// <summary>
        /// 转化成Canonical字符串(英文）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string TocCanonicalName(this string str)
        {
            return "";
        }

        /// <summary>
        /// 是否无效字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsInvalid(this string str)
        {
            return (str.IsNull() || str.Trim() == string.Empty);
        }

        /// <summary>
        /// 转化成Int,转化失败返回0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ParseInt(this string str)
        {
            try
            {
                return Convert.ToInt32(str.Trim());
            }
            catch
            {
                return 0;
            }

        }

        /// <summary>
        /// 转化成double
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static double ParseDouble(this string str)
        {
            try
            {
                return Convert.ToDouble(str.Trim());
            }
            catch
            {
                return 0.0d;
            }
        }

        /// <summary>
        /// 转化成Bool
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ParseBool(this string str)
        {
            try
            {
                return Convert.ToBoolean(str.Trim().ParseInt());
            }
            catch
            {
                return Boolean.Parse(str.Trim());
            }
        }

        /// <summary>
        /// 转化成Guid，转化失败返回Guid.Empty
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Guid ParseGuid(this string str)
        {
            try
            {
                return new Guid(str.Trim());
            }
            catch
            {
                return Guid.Empty;
            }
        }

        /// <summary>
        /// 转化成时间类型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ParseDatetime(this string str)
        {
            try
            {
                return Convert.ToDateTime(str.Trim());
            }
            catch
            {
                return DateTime.MinValue;
            }
        }
    }
}
